/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GestionBDD;

import Model.*;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Sik
 */
public class ControllerBDD
{
    private static final ControllerBDD INSTANCE = new ControllerBDD();
    
    private ControllerBDD()
    {
    }
    
    public List<Carte> importMap()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Carte.findAll");
        t.commit();
        return q.list();
    }
    
    public List<Zone> importZone()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Zone.findAll");
        t.commit();
        return q.list();
    }
    
    public List<Roles> importRole()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Roles.findAll");
        t.commit();
        return q.list();
    }
    
    public List<Utilisateur> importUtilisateur()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Utilisateur.findAll");
        t.commit();
        return q.list();
    }
    
    public void add(Utilisateur u)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(u);
        t.commit();
    }
    
    public void add(Roles r)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(r);
        t.commit();
    }
    
    public void remove(Utilisateur u)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(u);
        tx.commit();
    }
    
    public void update(Utilisateur u)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(u);
        tx.commit();
    }
    
    public void add(Carte carte)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(carte);
        t.commit();
    }
    
    public void add(Zone z)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(z);
        t.commit();
    }
    
    public void add(Attribution a)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(a);
        t.commit();
    }
    
    public void add(Epingle e)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(e);
        t.commit();
    }
    
    public void remove(Carte c)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(c);
        tx.commit();
    }
    
    public void remove(Zone z)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(z);
        tx.commit();
    }
    
    public void remove(Epingle e)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(e);
        tx.commit();
    }
    
    public void update(Carte c)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(c);
        tx.commit();
    }
    
    public void update(Zone z)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(z);
        tx.commit();
    }
    
    public void update(Epingle e)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(e);
        tx.commit();
    }

    public static ControllerBDD getINSTANCE()
    {
        return INSTANCE;
    }
}
